﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Data;

using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;

using DotNetNuke.Entities.Users;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DataTypeDateTime : DataTypeDate
  {




    [StructLayout(LayoutKind.Sequential)]
    private struct FieldSetting
    {
      public string Title;
      public bool NormalizeFlag;
      public string FormatString;
    }




    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      ArrayList fields = new ArrayList();
      FieldSetting field;
      string columnName;

      System.Web.HttpContext.Current.Trace.Write("enter DataTypeDateTime.RenderValuesToHtmlInsideDataSet");

      foreach(DataRow current in ds.Tables[DataSetTableName.Fields].Rows)
      {
        if( (string)current[FieldsTableColumn.Type] == this.Name )
        {
          field = new FieldSetting()
          {
            NormalizeFlag = Utilities.CBoolN((current[FieldsTableColumn.NormalizeFlag]), false),
            Title = (string)current[FieldsTableColumn.Title],
            FormatString = Utilities.CStrN((current[FieldsTableColumn.OutputSettings]), "g")
          };
          fields.Add(field);
          columnName = field.Title + DataTableColumn.Appendix_LocalizedValue;
          ds.Tables[DataSetTableName.Data].Columns.Add(columnName, typeof(string));
          ds.Tables[DataSetTableName.Data].Columns.Add(field.Title + DataTableColumn.Appendix_Ticks, typeof(long));
          current[FieldsTableColumn.ValueColumn] = XmlConvert.EncodeName(columnName);
        }
      }

      UserInfo currentUserInfo = UserController.GetCurrentUserInfo();

      UserTime time = new UserTime();
      DateTime dt;

      System.Web.HttpContext.Current.Trace.Write("DataTypeDateTime.ds.Tables[DataSetTableName.Data].Rows.Count " + ds.Tables[DataSetTableName.Data].Rows.Count.ToString());

      foreach(DataRow row2 in ds.Tables[DataSetTableName.Data].Rows)
      {
        System.Web.HttpContext.Current.Trace.Write("DataTypeDateTime.fields.Count " + fields.Count.ToString());
        foreach (FieldSetting fld in fields)
        {
          System.Web.HttpContext.Current.Trace.Write("DataTypeDateTime.fld.Title = " + fld.Title);

          if (row2[fld.Title] is DateTime)
          {
            System.Web.HttpContext.Current.Trace.Write("prepare DateTime row");
            dt = (DateTime)row2[fld.Title];

            System.Web.HttpContext.Current.Trace.Write("fld.NormalizeFlag && System.Web.HttpContext.Current.Request.IsAuthenticated = " + (fld.NormalizeFlag && System.Web.HttpContext.Current.Request.IsAuthenticated).ToString());

            if (fld.NormalizeFlag && System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
              System.Web.HttpContext.Current.Trace.Write("try time.ConvertToUserTime");
              dt = time.ConvertToUserTime(dt, (double)currentUserInfo.Profile.TimeZone);
              System.Web.HttpContext.Current.Trace.Write("time.ConvertToUserTime ok");
              row2[fld.Title] = dt;
            }
            row2[fld.Title + DataTableColumn.Appendix_LocalizedValue] = string.Format("<!--{0:000000000000}-->{1:" + fld.FormatString + "}", ((double)dt.Ticks) / 10000000.0, dt);
            row2[fld.Title + DataTableColumn.Appendix_Ticks] = dt.Ticks;
            System.Web.HttpContext.Current.Trace.Write("prepare DateTime row ok");
          }

        }
      }
    }




    public override void SetStylesAndFormats(BoundField Column, string Format)
    {
      base.SetStylesAndFormats(Column, Format);

      if ( !string.IsNullOrEmpty(Format) )
        Column.DataFormatString = string.Format("{{0:{0}}}", Format);

      else
        Column.DataFormatString = "{0:g}";

    }




    public override string Name
    { get { return "DateTime"; } }



    public override bool SupportsDefaultValue
    { get { return true; } }




    public override bool SupportsInputSettings
    { get { return false; } }



    public override bool SupportsNormalizeFlag
    { get { return true; } }



    public override bool SupportsOutputSettings
    { get { return true; } }



    public override bool SupportsValidation
    { get { return true; } }



  }


}


